package javabase.collection.list.listsort;

import java.util.Arrays;
import java.util.Comparator;

class Student0 {
	private String name;
	private int score;
	private int age;
	public Student0() {
		
	}
	public Student0(String name, int score, int age) {
		super();
		this.setName(name);
		this.setScore(score);
		this.setAge(age);
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getScore() {
		return score;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", score=" + score + ", age=" + age + "]";
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + score;
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		
		if(this == obj) {
			return true;
		}
		if(obj == null) {
			return false;
		}
		if(getClass() != obj.getClass()) {
			return false;
		}
		Student0 other = null;
		if (!(obj instanceof Student0)) {
			return false;
		}
		other = (Student0)obj;
		
		if(this.name == null) {
			if(other.name != null) {
				return false;
			} 
		} else if (!this.name.equals(other.getName())) {
			return false;
		}
		
		if(this.score != other.score) {
			return false;
		}
		
		if(this.age != other.age) {
			return false;
		}
		return true;
	}
	
	
}

class MyComparator implements Comparator<Student> {

	public int compare(Student o1, Student o2) {
		if(o1.getScore()>o2.getScore()) {
			return 1;
		} else if(o1.getScore()>o2.getScore()) {
			return -1;
		} else {
			if(o1.getAge()>o2.getAge()) {
				return 1;
			} else if(o1.getAge()<o2.getAge()) {
				return -1;
			} else {
				return 0;
			}
		}
	}
	
}

public class ComparatorDemo01 {
	public static void main(String[] args) {
		Student [] stu = new Student[5];
		stu[0] = new Student("小明",96,10);
		stu[1] = new Student("小明",96,10);
		stu[2] = new Student("小孙",55,9);
		stu[3] = new Student("小李",55,11);
		stu[4] = new Student("小猛",99,19);
//		System.out.println(stu[0].equals(stu[0]));
		System.out.println("排序之前：");
		for(int i=0;i<stu.length;i++) {
			System.out.println(stu[i]);
		}
		Arrays.sort(stu,new MyComparator());
		System.out.println("排序之后：");
		for(int i=0;i<stu.length;i++) {
			System.out.println(stu[i]);
		}
	}

}
